window["_dc_popup_sidebar_all_id"]=[]
popup_sidebar = {
    //id 弹窗标识
    //data 弹窗配置
    //cb 回调函数 关闭函数，点击弹窗外函数，弹窗内点击函数
    init: function (data) {
        //总窗口id记录
        window["_dc_popup_sidebar_all_id"].push(data.id);
        window["_dc_popup_sidebar_data_"+data.id] = data; 
        //判断弹窗方向并给动画
        switch (data.direction) {
            case 'left':
                var open = "popuptoleft-ani";
                var close = "closetoleft-ani";
                break;
            case 'right':
                var open = "popuptoright-ani";
                var close = "closetoright-ani";
                break;
            case 'bottom':
                var open = "popuptotop-ani";
                var close = "closetotop-ani";
                break;
            case 'top':
                var open = "popuptobottom-ani";
                var close = "closetobottom-ani";
                break;
            default:
                break;
        }
        //给默认
        if(data.width==undefined){
            data.width='100%';
        }
        if(data.height==undefined){
            data.height='40vh';
        }
        if(data.style==undefined){
            data.style='background-color: #000;opacity: 0.9;color: #fff;';
        }
        $("#"+data.id).prepend(`
        <!-- ugc / 非全屏弹出框 start-->
        <div class="popup_${data.id}" data-openani='${open}' data-closeani='${close}'
            style="${data.style} position: fixed;bottom: -${data.height};height: ${data.height};width: ${data.width};">
            <i style="${data.close_style}" class="close close_${data.id}">&#10005;</i>
            ${data.content}
        </div>
        <!-- ugc / 非全屏弹出框 end-->
        `);

        //阻止弹出框的点击事件冒泡
        $(`.popup_${data.id}`).click(function (event) {
            event.stopPropagation();
            data.callback.click({html:this,id:data.id});
        });
        //点击除了弹出框以及触发弹出框弹出的按钮，使弹出框的返回
        $(document).click(function (event) {
            
            var popup = $(`.popup_${data.id}`),
                popup_closeani = popup.data('closeani'),
                popup_openani=popup.data('openani'),
                target = $(event.target);
            if ($(target).hasClass('popup')) {
                return false;
            }
            //如果当前弹出框是出现状态，则点击后触发弹框返回
            if (popup.hasClass(popup_openani)) {
                data.callback.click_out_popup_sidebar({html:popup,id:data.id});
                popup.removeClass(popup_openani).addClass(popup_closeani);
            }
            console.log("完成")
            
        });
        //弹出框的关闭按钮事件
        $(`.close_${data.id}`).on('click', function (event) {
            //阻止冒泡
            event.stopPropagation();
            var popup = $(`.popup_${data.id}`),
            popup_closeani = popup.data('closeani'),
            popup_openani=popup.data('openani');
            data.callback.click_close_popup_sidebar({html:popup,id:data.id});
            popup.removeClass(popup_openani).addClass(popup_closeani);
        });
        return;
    },
    
    popup: function (data) {
        console.log('弹窗了');
        if(data.direction!=undefined){

        }

        console.log(data.id);
        var popup = $(`.popup_${data.id}`),
        popup_closeani = popup.data('closeani'),
        popup_openani  = popup.data('openani');
        popup.removeClass(popup_openani).removeClass(popup_closeani);
        setTimeout(function () {
            popup.addClass(popup_openani);
        });
        if(data.time!=undefined){
            clearTimeout(window["popup_"+data.id+"_time"]);
            window["popup_"+data.id+"_time"]=setTimeout(function(){ popup_sidebar.close(data) },data.time);
        }
    },
    close_popup: function (data) {
        console.log('关弹窗了');
        console.log(data.id);
        var popup = $(`.popup_${data.id}`),
        popup_closeani = popup.data('closeani');
        popup_openani=popup.data('openani');
        if (popup.hasClass(popup_openani)) {
            console.log("关闭弹窗")
            popup.removeClass(popup_openani).addClass(popup_closeani);
            window["_dc_popup_sidebar_data_"+data.id].callback.click_close_popup_sidebar({html:popup,id:data.id});
        }
    },
    popup_close_all(data){
        data.forEach(element => {
            console.log(element)
            var popup = $(`.popup_${element}`);
            popup_closeani = popup.data('closeani');
            popup_openani=popup.data('openani');
            if (popup.hasClass(popup_openani)) {
                console.log("关闭弹窗")
                popup.removeClass(popup_openani).addClass(popup_closeani);
            }
        });
    }
}